
#ifdef MBM_IMIBASE_SUPPORT
#include "mbm_imibase_id.h"

#include "mbm_spreadtrum_inc.h"
#include "mbm_typedefine.h"
#include "mbm_adapter.h"

//audio test
#include "mbm_typedefine.h"
#include "irpmw_mm.h"


#if defined(MBM_DEBUG_ENG)
#define MBM_ENG_TRACE mbm_trace
#else
#define MBM_ENG_TRACE 
#endif

typedef struct
{
	MMI_IMAGE_ID_T icon_id;
	const uint8 *str_ptr;
	void (*entry)(uint8*);
} t_mbm_eng_list_info;

static MMI_RESULT_E HandleShowImiBaseVersionWinMsg(MMI_WIN_ID_T win_id, MMI_MESSAGE_ID_E msg_id, DPARAM param);

#define CREATE_ENG_LIST_WIN(NAME, Name)	\
static MMI_RESULT_E HandleMbmEng##Name##WinMsg(MMI_WIN_ID_T win_id, MMI_MESSAGE_ID_E msg_id, DPARAM param); \
WINDOW_TABLE(MBM_ENG_##NAME##_WIN_TAB) = \
{\
	WIN_FUNC((uint32)HandleMbmEng##Name##WinMsg),    \
	WIN_ID(MBM_ENG_##NAME##_WIN_ID),\
	WIN_TITLE(TXT_NULL),\
	CREATE_LISTBOX_CTRL(0, MMI_TITLE_HEIGHT, MMI_MAINSCREEN_RIGHT_MAX_PIXEL, \
		MMI_MAINSCREEN_BOTTOM_MAX_PIXEL - MMI_FULLSCREEN_SOFTKEY_BAR_HEIGHT, \
		GUILIST_TEXTLIST_E, MBM_ENG_##NAME##WIN_LIST_CTRL_ID),\
	WIN_SOFTKEY(STXT_OK, TXT_NULL, STXT_RETURN),\
	END_WIN\
};\
static MMI_RESULT_E HandleMbmEng##Name##WinMsg(MMI_WIN_ID_T win_id, MMI_MESSAGE_ID_E msg_id, DPARAM param)\
{\
	MMI_RESULT_E result = MMI_RESULT_TRUE;\
	MMI_CTRL_ID_T ctrl_id = MBM_ENG_##NAME##WIN_LIST_CTRL_ID;	\
	char *title = "Mbm Eng "#Name;\
	wchar wtitle[64];\
	uint32 title_len;\
	int32 i;\
	switch(msg_id) \
	{\
	case MSG_OPEN_WINDOW:\
		GUILIST_SetMaxItem( ctrl_id, ARR_SIZE(s_eng##Name##List), FALSE );\
		for(i=0; i<ARR_SIZE(s_eng##Name##List); i++)\
		{\
			AppendEngListItem(ctrl_id, s_eng##Name##List[i].icon_id, s_eng##Name##List[i].str_ptr);\
		}\
		title_len = MIN(ARR_SIZE(wtitle), strlen(title));\
		MMI_STRNTOWSTR(wtitle, title_len, title, title_len, title_len); \
		GUIWIN_SetTitleText(win_id, wtitle, title_len, FALSE);\
		MMK_SetAtvCtrl( win_id, ctrl_id);\
		break;\
	case MSG_CTL_PENOK:\
	case MSG_CTL_MIDSK:\
	case MSG_CTL_OK:\
	case MSG_APP_OK:\
	case MSG_APP_WEB:\
		if(s_eng##Name##List[GUILIST_GetCurItemIndex(ctrl_id)].entry)\
		{\
			(*s_eng##Name##List[GUILIST_GetCurItemIndex(ctrl_id)].entry)(s_eng##Name##List[GUILIST_GetCurItemIndex(ctrl_id)].str_ptr);\
		}\
		break;\
	case MSG_CTL_CANCEL:\
	case MSG_APP_CANCEL:\
		MMK_CloseWin(win_id);\
		break;\
	default:\
		result = MMI_RESULT_FALSE;\
		break;\
	}\
	return (result);\
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

static void AppendEngListItem(MMI_CTRL_ID_T ctrl_id, MMI_IMAGE_ID_T icon_id, const char *str_ptr);

static void EntryShowImiBaseVersionWin(uint8 *title);
static void EntryImibaseDevVersionWin(uint8 *title);



WINDOW_TABLE(MMIENG_SHOWIMIVERSION_WIN_TAB) = 
{
	//CLEAR_LCD,
	//WIN_PRIO( WIN_ONE_LEVEL ),
	WIN_FUNC((uint32)HandleShowImiBaseVersionWinMsg),
	WIN_ID(MBM_ENG_SHOW_IMIBASEVERSION_WIN_ID),
	//WIN_TITLE(TXT_ENG_PHONE_INFO),
    WIN_SOFTKEY(STXT_OK, TXT_NULL, STXT_RETURN),
	CREATE_TEXTBOX_CTRL(ENG_CLIENT_LEFT, ENG_CLIENT_TOP, ENG_CLIENT_RIGHT, ENG_CLIENT_BOTTOM, MAIN_LCD_ID, MBM_ENG_IMIBASEVERSION_CTRL_ID),
	END_WIN
};
//xiaohua #endif
static t_mbm_eng_list_info s_engMainList [] =
{
	{IMAGE_NUMBER_1, "show imibase version", EntryShowImiBaseVersionWin},
       {IMAGE_NUMBER_2, "show imibase dev version", EntryImibaseDevVersionWin},     //  for debug
 };

CREATE_ENG_LIST_WIN(MAIN,Main)

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

void MMIMBM_ProcessEng()//uint8* str_ptr, uint16 str_len)
{

MMK_CreateWin((uint32*)MBM_ENG_MAIN_WIN_TAB, PNULL);

}

static void AppendEngListItem(MMI_CTRL_ID_T ctrl_id, MMI_IMAGE_ID_T icon_id, const char *str_ptr)
{
	GUILIST_ITEM_T 		item_t = {0};
	GUILIST_ITEM_DATA_T item_data = {0};
	uint32 len;

	SCI_ASSERT(str_ptr);
	    
	item_t.item_style    = GUIITEM_STYLE_ONE_LINE_ICON_TEXT;
	item_t.item_data_ptr = &item_data;

	item_data.item_content[0].item_data_type = GUIITEM_DATA_IMAGE_ID;
	item_data.item_content[0].item_data.image_id = icon_id;
    
	item_data.item_content[1].item_data_type = GUIITEM_DATA_TEXT_BUFFER;
	item_data.item_content[1].item_data.text_buffer.wstr_len = len = MIN(strlen(str_ptr), GUILIST_STRING_MAX_NUM );
	MMI_STRNTOWSTR(item_data.item_content[1].item_data.text_buffer.wstr, GUILIST_STRING_MAX_NUM, str_ptr, len, len);
        
	GUILIST_AppendItem( ctrl_id, &item_t );    
}



static void EntryShowImiBaseVersionWin(uint8 *title)
{
       MMI_STRING_T text = {0};
	wchar* info=VERSION_TEXT;
      uint32 title_len = SCI_STRLEN("Mbm Eng ") + SCI_STRLEN(title);
	wchar *title_buf = (wchar*)MBM_Malloc((title_len + 1) * 2);

	
	MMK_CreateWin((uint32*)MMIENG_SHOWIMIVERSION_WIN_TAB, PNULL);

	MMI_STRNTOWSTR(title_buf, title_len, "Mbm Eng ", SCI_STRLEN("Mbm Eng "), SCI_STRLEN("Mbm Eng ")); 
	MMI_STRNTOWSTR(title_buf + SCI_STRLEN("Mbm Eng "), SCI_STRLEN(title), title, SCI_STRLEN(title), SCI_STRLEN(title)); 

	GUIWIN_SetTitleText(MBM_ENG_SHOW_IMIBASEVERSION_WIN_ID, title_buf, title_len, FALSE);
			
	text.wstr_ptr = info;
	text.wstr_len = MMIAPICOM_Wstrlen(info);
	GUITEXTBOX_SetText(MBM_ENG_IMIBASEVERSION_CTRL_ID, &text, FALSE);
	
	MBM_Free(title_buf);
}

static MMI_RESULT_E HandleShowImiBaseVersionWinMsg(MMI_WIN_ID_T win_id, MMI_MESSAGE_ID_E msg_id, DPARAM param)
{
//	uint8 buf[251] = {0};
  //  wchar wstr[251] = {0};
 //   char* str = PNULL;
//	MMI_STRING_T text = {0};
	MMI_RESULT_E result = MMI_RESULT_TRUE;
	
	switch (msg_id) 
	{
	case MSG_OPEN_WINDOW:
		MMK_SetAtvCtrl(MBM_ENG_SHOW_IMIBASEVERSION_WIN_ID, MBM_ENG_IMIBASEVERSION_CTRL_ID);
		break;
		
	case MSG_FULL_PAINT:
	//	GUIWIN_SetSoftkeyTextId(MBM_ENG_SHOW_IMIBASEVERSION_WIN_ID,  (MMI_TEXT_ID_T)TXT_ENG_OK, (MMI_TEXT_ID_T)TXT_NULL, (MMI_TEXT_ID_T)TXT_ENG_RETURN, FALSE);
		break;
	case MSG_APP_WEB:		
	case MSG_KEYDOWN_OK:
	case MSG_KEYDOWN_CANCEL:
		MMK_CloseWin(MBM_ENG_SHOW_IMIBASEVERSION_WIN_ID);
		break;
	
	default:
		result = MMI_RESULT_FALSE;
		break;
	}
	return (result);
}

static void ShowTextWin(uint8 *title, uint8 *str)
{
	MMI_STRING_T text;
	uint32 title_len = SCI_STRLEN("Mbm Eng ") + SCI_STRLEN(title);
	uint32 str_len = SCI_STRLEN(str);	
	wchar *buf = MBM_Malloc(MAX(title_len, str_len) * 2);

	MMK_CreateWin((uint32*)MMIENG_SHOWIMIVERSION_WIN_TAB, PNULL);
			
	MMI_STRNTOWSTR(buf, title_len, "Mbm Eng ", SCI_STRLEN("Mbm Eng "), SCI_STRLEN("Mbm Eng ")); 
	MMI_STRNTOWSTR(buf + SCI_STRLEN("Mbm Eng "), SCI_STRLEN(title), title, SCI_STRLEN(title), SCI_STRLEN(title)); 
	GUIWIN_SetTitleText(MBM_ENG_SHOW_IMIBASEVERSION_WIN_ID, buf, title_len, FALSE);
			
	MMI_STRNTOWSTR(buf, str_len, str, str_len, str_len); 
	text.wstr_ptr = buf;
	text.wstr_len = str_len;
	GUITEXTBOX_SetText(MBM_ENG_IMIBASEVERSION_CTRL_ID, &text, FALSE);

	MBM_Free(buf);
}


extern const char *g_imibase_date;
extern const char *g_imibase_revision;
extern const char *g_imibase_svn;

void EntryImibaseDevVersionWin(uint8 *title)
{
    uint8 *str;
    
    str = (uint8*)MBM_Malloc(500);

    memset(str, 0 ,500);

    sprintf(str, "imibase svn :%s\n", g_imibase_svn);
    sprintf(str+strlen(str), "imibase revision:%s\n", g_imibase_revision);
    sprintf(str+strlen(str), "imibase compile date:%s\n", g_imibase_date);
    
    ShowTextWin(title, str);
    MBM_Free(str);    
}
 
#endif/*MBM_IMIBASE_SUPPORT*/

